﻿using System;
using System.Collections.Generic;
using System.Linq;
using Model.Repositories;
using DomenModel;
using System.Data.Entity;
using System.Linq.Expressions;
using Model.Repositories.Interfaces;

namespace Infrastructure.Repositories
{
    public class ItemDimensionRepo : ICrudItemDimension
    {
        /// <summary>
        /// Gets the ItemDimension by id.
        /// </summary>
        /// <param name="Id">The id.</param>
        /// <returns></returns>
        public ItemDimension GetById(long Id)
        {
            using (GeneralContext _db = new GeneralContext())
            {
                ItemDimension itemDimensionById = _db.ItemDimensions.FirstOrDefault(p => p.ItemDimensionId == Id);
                return itemDimensionById;
            }
        }

        /// <summary>
        /// Saves the ItemDimension to data base.
        /// </summary>
        /// <param name="t">The ItemDimension.</param>
        public void Save(ItemDimension t)
        {
            using (GeneralContext _db = new GeneralContext())
            {
                
                _db.ItemDimensions.Add(t);
                _db.SaveChanges();
            }

        }

        /// <summary>
        /// Updates the ItemDimension.
        /// </summary>
        /// <param name="t">The ItemDimension.</param>
        public void Update(ItemDimension t)
        {
            using (GeneralContext _db = new GeneralContext())
            {
                ItemDimension itemDimensionById = _db.ItemDimensions.FirstOrDefault(p => p.ItemDimensionId == t.ItemDimensionId);
                itemDimensionById.ItemId = t.ItemId;
                itemDimensionById.Item = t.Item;
                itemDimensionById.Price = t.Price;
                itemDimensionById.Quantity = t.Quantity;
                itemDimensionById.Dimension = t.Dimension;
                _db.SaveChanges();
            }

        }

        /// <summary>
        /// Deletes the ItemDimension from data base by id.
        /// </summary>
        /// <param name="Id">The id.</param>
        public void Delete(long Id)
        {
            using (GeneralContext _db = new GeneralContext())
            {
                ItemDimension itemDimensionById = _db.ItemDimensions.FirstOrDefault(p => p.ItemDimensionId == Id);
                _db.ItemDimensions.Remove(itemDimensionById);
                _db.SaveChanges();
            }
        }

        /// <summary>
        /// Gets the ItemDimension by price.
        /// </summary>
        /// <param name="price">The price.</param>
        /// <returns></returns>
        public ItemDimension GetByPrice(decimal price)
        {
            using (GeneralContext _db = new GeneralContext())
            {
                ItemDimension itemDimensionByPrice = _db.ItemDimensions.FirstOrDefault(p => p.Price == price);
                return itemDimensionByPrice;
            }
        }

        /// <summary>
        /// Gets the ItemDimension by quantity.
        /// </summary>
        /// <param name="quantity">The quantity.</param>
        /// <returns></returns>
        public ItemDimension GetByQuantity(int quantity)
        {
            using (GeneralContext _db = new GeneralContext())
            {
                ItemDimension itemDimensionByQuantity = _db.ItemDimensions.FirstOrDefault(p => p.Quantity == quantity);
                return itemDimensionByQuantity;
            }
        }

        /// <summary>
        /// Gets the collection of all ItemDimension.
        /// </summary>
        /// <returns></returns>
        public List<ItemDimension> GetCollection()
        {
            using (GeneralContext _db = new GeneralContext())
            {
                var listOfItemDimension = _db.ItemDimensions.ToList();
                return listOfItemDimension;
            };
        }

     }
}
